package com.webserver.http;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
* 响应对象
* 该类的每一个实例用于表示一个HTTP的响应内容
* 每个响应由三部分构成
* 状态行，响应头，消息正文
* */
public class HttpResponse {
    /*
    * 将当前响应对象内容按照标准的HTTP响应格式发送给客户端
    * */
    //状态行相关信息
    private String protocol = "HTTP/1.1";//协议版本
    //默认200，OK的原因是大部分响应都是正常的
    private int statusCode = 200;//状态代码
    private String statusReason = "OK";//状态描述

    //响应头相关信息
    //key响应头的名字 value响应头的值
    private Map<String,String> headers = new HashMap<>();

    //响应正文相关信息
    private File entily;//正文对应的一个实际文件

    private Socket socket;
    public HttpResponse(Socket socket){
        this.socket = socket;
    }
    public void flush() throws IOException{
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //3.3发送正文
        sendContent();
    }
    private void sendStatusLine() throws IOException {
        String line = protocol+" "+statusCode+" "+statusReason;
        println(line);
    }
    private void sendHeaders() throws IOException {
//        String line = "Content-Type: text/html";
//        println(line);
//
//        line = "Content-Length: "+entily.length();
//        println(line);
        Set<Map.Entry<String ,String>> entrySet = headers.entrySet();
        for(Map.Entry<String,String> e : entrySet ){
            String name = e.getKey();
            String value = e.getValue();
            String line = name+": "+value;
            println(line);
        }
        //单独发送CRLF表示响应头发送完毕
        println("");
        /*相当于
            out.write(13);
            out.write(10);
            */
    }
    private void sendContent() throws IOException {
        OutputStream out = socket.getOutputStream();
        try (FileInputStream fis = new FileInputStream(entily);
        ) {
            byte[] buf = new byte[1024 * 8];
            int len;
            while ((len = fis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
        }
    }

    private void println(String line) throws IOException {
        byte[] data = line.getBytes("ISO8859-1");
        OutputStream out = socket.getOutputStream();
        out.write(data);
        out.write(13);
        out.write(10);
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntily() {
        return entily;
    }

    public void setEntily(File entily) {
        this.entily = entily;
    }
    public void addHeader(String name,String value){
        this.headers.put(name,value);
    }
}
